<template>
	<div class="system-dept-container">
		<el-card shadow="hover" class="mb10" v-auth="'system:dept:list'">
			<el-form :inline="true" :model="search">
				<el-form-item>
					<el-input v-model="search.deptName" placeholder="请输入部门名称" prefix-icon="el-icon-search"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button icon="el-icon-search" type="primary" @click="onTableSearch()">查询</el-button>
					<el-button icon="el-icon-refresh" @click="onResetSearch()">重置</el-button>
				</el-form-item>
			</el-form>
		</el-card>
		<el-card shadow="hover">
			<el-button v-auth="'system:dept:add'" type="primary" class="ml10" icon="el-icon-document-add" @click="onOpenAddDept()">新增</el-button>
			<el-table ref="tableDeptRef" :data="tableData.data" stripe style="width: 100%" row-key="id" :tree-props="{ children: 'children'}">
				<el-table-column label="部门名称" prop="deptName" min-width="200" show-overflow-tooltip></el-table-column>
				<el-table-column label="负 责 人" prop="leader" width="100" show-overflow-tooltip></el-table-column>
				<!-- <el-table-column label="联系电话" prop="phone" width="100" show-overflow-tooltip></el-table-column> -->
				<!-- <el-table-column label="邮箱账号" prop="email" width="100" show-overflow-tooltip></el-table-column> -->
				<el-table-column label="状态" show-overflow-tooltip width="70">
					<template #default="scope">
						<span v-if="scope.row.status == '0'" class="color-primary">正常</span>
						<span v-else-if="scope.row.status == '1'" class="color-info">停用</span>
						<span v-else class="color-danger">删除</span>
					</template>
				</el-table-column>
				<el-table-column label="部门排序" prop="orderNum" width="80" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column label="备注" prop="remark" min-width="100" show-overflow-tooltip></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="155" 
					v-auths="['system:dept:add', 'system:dept:edit', 'system:dept:remove']">
					<template #default="scope">
						<el-button v-auth="'system:dept:add'" type="primary" link @click="onOpenAddDept(scope.row)">新增</el-button>
						<el-button v-auth="'system:dept:edit'" type="primary" link @click="onOpenEditDept(scope.row)">修改</el-button>
						<el-button v-auth="'system:dept:remove'" type="primary" link @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddDept ref="addDeptRef" @submit="onTableSearch"/>
		<EditDept ref="editDeptRef" @submit="onTableSearch"/>
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import AddDept from '/@/views/system/dept/addDept.vue';
import EditDept from '/@/views/system/dept/editDept.vue';
import { getDeptList, delDept } from '/@/api/system/dept/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
	name: 'dept',
	components: { AddDept, EditDept },
	setup() {
		const tableDeptRef = ref();
		const addDeptRef = ref();
		const editDeptRef = ref();
		const state = reactive({
			search:{
				deptName: undefined
			},
			tableData: {
				data: [],
				total: 0,
				loading: false
			}
		});

		const loadTableData = (params) => {
			getDeptList(params).then((res) => {
				if(res.code == 0){
					state.tableData.data = lineToTreeData(res.data);
					state.tableData.total = res.total;
				}
			});
		}

		//查询
		const onTableSearch = () => {
			loadTableData(state.search);
		}
		//重置
		const onResetSearch = () => {
			state.search.deptName = undefined;
			loadTableData();
		}
		// 打开新增部门弹窗
		const onOpenAddDept = (row: object) => {
			addDeptRef.value.openDialog(row);
		};
		// 打开编辑部门弹窗
		const onOpenEditDept = (row: object) => {
			editDeptRef.value.openDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: object) => {
			ElMessageBox.confirm('确定删除该条【' + row.deptName + '】部门信息吗？', '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			}).then(() => {
				delDept([row.id]).then((res) => {
					if(res.code === 0){
						ElMessage.success('删除成功');
						onTableSearch();
					}
				});
			}).catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			loadTableData();
		});
		return {
			tableDeptRef,
			addDeptRef,
			editDeptRef,
			onTableSearch,
			onResetSearch,
			onOpenAddDept,
			onOpenEditDept,
			onTabelRowDel,
			...toRefs(state),
		};
	},
};
</script>